#include "vtkmethods.h"

namespace vtkMethods
{

	DECLDIR vtkSmartPointer<vtkActor> vtkloadstl(std::string fileName)
	{
		// Initialize objects
		vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
		vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
		vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();

		// Set filename in reader
		reader->SetFileName(fileName.c_str());
		reader->Update();

		// Set mapper input connection to reader output port
		mapper->SetInputConnection(reader->GetOutputPort());

		// Set mapper to actor
		actor->SetMapper(mapper);

		// Return stlActor
		return actor;

	}

	DECLDIR vtkSmartPointer<vtkTransformCollection> vtkImportJTS(std::string fileName)
	{
		// Create vtkTransformCollection to store vtkTransformation data
		vtkSmartPointer<vtkTransformCollection> transformCollection = vtkSmartPointer<vtkTransformCollection>::New();
		
		// Open file
		std::ifstream inFile;
		inFile.open(fileName.c_str());
		//FILE *inFile;
		//inFile = fopen(fileName.c_str(),"r");

		if (inFile.is_open())
		{
			// Read and discard first to lines of file
			std::string str;
			getline(inFile,str); // First line is junk
			getline(inFile,str); // Second line is also junk
			//fscanf(inFile,"%s",str);
			//fscanf(inFile,"%s %s %s %s %s %s",str,str,str,str,str,str);

			// Create vtkTransform
			vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
			transform->Identity();

			// Create pose vector
			float trans[3]; trans[0] = 0; trans[1] = 0; trans[2] = 0;
			float rot[3]; rot[0] = 0; rot[1] = 0; rot[2] = 0;

			// Read data until the end of file
			//while (!feof(inFile)) 
			while(!inFile.is_open())
			{
				// Read in translations
				//inFile >> trans[0] >> trans[1] >> trans[2];
				// .. and then the rotations
				//inFile >> rot[0] >> rot[1] >> rot[2];

				inFile >> trans[0];
				inFile >> trans[1];
				inFile >> trans[2]; 
				inFile >> rot[0];
				inFile >> rot[1];
				inFile >> rot[2];

				//fscanf(inFile,"%f %f %f %f %f %f",input,input+1,input+2,input+3,input+4,input+5);

				// Rotate transform (312) then Translate
				transform->RotateZ(rot[0]);
				transform->RotateX(rot[1]);
				transform->RotateY(rot[2]);
				transform->Translate(trans[0],trans[1],trans[2]);

				// Add transfrom to transformCollection
				transformCollection->AddItem(transform);

				// Set transform to identity
				transform->Identity();
				
			} // END WHILE STATEMENT
			

			//inFile.close();
			//fclose(inFile);

		} // END IF STATEMENT

		return transformCollection;
	}

}